<?php
/**
 * Room Model for RoomBooking Component
 *
 * @version    1.0
 * @package    Joomla
 * @subpackage RoomBooking
 * @copyright (C) 2011 Giovanni Marzulli
 * @license	   GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');

class RoomBookingModelRoom extends JModel
{
	/**
	 * Constructor that retrieves the ID from the request
	 *
	 * @access	public
	 * @return	void
	 */
	function __construct()
	{
		parent::__construct();

		$array = JRequest::getVar('cid',  0, '', 'array');
		$this->setId((int)$array[0]);
	}

	/**
	 * Method to set the identifier
	 *
	 * @access	public
	 * @param	int identifier
	 * @return	void
	 */
	function setId($id)
	{
		// Set id and wipe data
		$this->_id		= $id;
		$this->_data	= null;
	}

	/**
	 * Method to get a room record
	 * @return object with data
	 */
	function &getData()
	{
		// Load the data
		if (empty( $this->_data )) {
			$query = ' SELECT * FROM #__roombooking_rooms '. 
					 ' WHERE id = '.$this->_id;
			$this->_db->setQuery( $query );
			$this->_data = $this->_db->loadObject();
		}
		if (!$this->_data) {
			$this->_data = new stdClass();
			$this->_data->id = 0;
                        $this->_data->room_type_id = null;
			$this->_data->name = null;
			$this->_data->floor = null;
			$this->_data->open = 1;
			$this->_data->id_building = null;
		}
		return $this->_data;
	}

	
	/**
	 * Method to get id of building room 
	 * @return int
	 */
	function getIdBuildOfRoom($id)
	{
		// Load the data
		$query = ' SELECT id_building  '
        	   . ' FROM #__roombooking_rooms WHERE id='.$id;
		$this->_db->setQuery( $query );
		$id_building = $this->_db->loadObject();
			
		return $id_building->id_building;
	}
	
	/**
	 * Method to store a record
	 *
	 * @access	public
	 * @return	boolean	True on success
	 */
	function store()
	{	
		$row =& $this->getTable();

		$data = JRequest::get( 'post' );

		// Bind the form fields to the room table
		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		// Make sure the room record is valid
		if (!$row->check()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		// Sets the error message
		if (!$row->store()) {
			$this->setError( $row->getErrorMsg() );
			return false;
		}

		return $row->id;
	}
	
	/**
	 * Method to delete record(s)
	 *
	 * @access	public
	 * @return	boolean	True on success
	 */
	function delete($cids)
	{
		JLoader::import( 'booking', JPATH_COMPONENT_ADMINISTRATOR . DS . 'models' );
		$modelBooking = JModel::getInstance( 'booking', 'RoomBookingModel' );
		
		if(!isset($cids))
			$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );

		$row =& $this->getTable();

		if (count( $cids )) {
			foreach($cids as $cid) {
				if (!$row->delete( $cid )) {
					$this->setError( $row->getErrorMsg() );
					return false;
				}
				$modelBooking->bookingCidsOfRoom($cid);
			}
		}
		return true;
	}
	
   /**
    * Gets room ids of a building
    * @return array Array of objects containing the data from the database
    */
	function roomCidsOfBuilding($id_building)
	{
		$query = ' SELECT id  '
        	   . ' FROM #__roombooking_rooms WHERE id_building='.$id_building;
		$this->_db->setQuery( $query );
		$cids = $this->_db->loadResultArray();
		
		$this->delete($cids);
	}

}